package com.why.blog.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.why.blog.bean.Blog;
import com.why.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @program: blog
 * @description:
 * @author: @why
 * @create: 2021-01-22 11:24
 **/
@Slf4j
@Controller
public class searchController {
    @Autowired
    private BlogService blog;


    @RequestMapping(value = "/search", method = {RequestMethod.POST, RequestMethod.GET})
    public String searchBlog(Model model,
                             @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNo,
                             @RequestParam(value = "blogName",required = false) String blogName) {

        log.error(blogName);
       if(blogName==null||"".equals(blogName)){
           log.error("sdaaaaaaaaa");
           model.addAttribute("msg", null);
           model.addAttribute("error","请输入博客的名字!");
           return "searchBlog";
       }

        pageNo = pageNo == 0 ? 1 : pageNo;
        PageHelper.startPage(pageNo, 3);
       if(!StringUtils.hasLength(blogName)||blogName.length()>30){
           return "redirect:index";
       }
        PageInfo<Blog> blogInfo = new PageInfo<Blog>(blog.getBlogByName(blogName));
        model.addAttribute("msg", blogInfo);
        model.addAttribute("blogName",blogName);
        return "searchBlog";
    }


}



